package com.cloudkinto.controller.orderoutput;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.fbaorderoutput.FbaOrderOutputService;
import com.cloudkinto.service.fbaorderoutput.vo.FbaOrderOutputAddReq;
import com.cloudkinto.service.fbaorderoutput.vo.FbaOrderOutputOpenAddReq;
import com.cloudkinto.service.orderoutput.OrderOutputService;
import com.cloudkinto.service.orderoutput.openapivo.OpenApiOrderOutputAddReq;
import com.cloudkinto.service.orderoutput.vo.OrderOutputDetailRes;
import com.cloudkinto.service.orderoutput.vo.OrderOutputListRes;
import com.cloudkinto.service.shop.ShopService;
import com.cloudkinto.service.stock.StockLogService;
import com.cloudkinto.service.stock.StockRecordService;
import com.cloudkinto.service.stock.vo.record.StockRecordPageRes;
import com.cloudkinto.service.storage.StorageService;
import com.cloudkinto.service.user.UserService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/9/8 15:59
 * @description: 对外抛出公共api
 */
@RestController
@Api(tags = "出库订单对外抛出api模块")
@RequestMapping("/api/open/orderOutput")
public class OrderOutputOpenApiController extends BaseController {
    @Autowired
    private OrderOutputService service;
    @Autowired
    private FbaOrderOutputService fbaService;
    @Autowired
    private StockRecordService stockRecordService;

    @Autowired
    private StockLogService stockLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private StorageService storageService;

    @PostMapping("getTokenLabella")
    @ApiOperation(value = "获取token", notes = "获取token")
    public SingleResult getTokenLabella(HttpServletRequest request, String username,String password) {
        return userService.getTokenLabella(username, password,request);
    }


    @PostMapping("getToken")
    @ApiOperation(value = "获取token", notes = "获取token")
    public SingleResult login(HttpServletRequest request, String username,String password) {
        return userService.getToken(username, password,request);
    }

    @GetMapping("getToken")
    @ApiOperation(value = "获取token", notes = "获取token")
    public SingleResult login2(HttpServletRequest request,String username, String password) {
        return userService.getToken(username, password, request);
    }

    @UserSessionRequired
    @PostMapping("add")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated OpenApiOrderOutputAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.openApiAdd(req, companyId, userId);
    }

    @UserSessionRequired
    @GetMapping("/orderOutListPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字搜索"), @ApiImplicitParam(name = "keyword", value = "关键字搜索"),
            @ApiImplicitParam(name = "current", value = "当前页"), @ApiImplicitParam(name = "pagesize", value = "页大小"),
            @ApiImplicitParam(name = "status", value = "订单状态"),
            @ApiImplicitParam(name = "trackId", value = "快递公司"), @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "shopId", value = "店铺id"), @ApiImplicitParam(name = "storageId", value = "仓库id")
            , @ApiImplicitParam(name = "startDate", value = "开始时间"), @ApiImplicitParam(name = "endDate", value = "结束时间")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = OrderOutputListRes.class)})
    public PageResult<OrderOutputListRes> listPage(String keyword, String current, String pageSize,
                                                   String status, String startDate, String endDate, String trackId, String shopId, String storageId) {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("status", status);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("trackId", trackId);
        map.put("storageId", storageId);
        map.put("orderType", "1");//一件代发
        if (!StringUtils.isEmpty(shopId)) {
            map.put("shopId", shopId);
        }
        map.put("companyId", companyId + "");
        return service.getListPage(map);
    }


    @UserSessionRequired
    @GetMapping("/stockRecordListPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺id"), @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "keyword", value = "模糊搜索"), @ApiImplicitParam(name = "productType", value = "商品分类")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = StockRecordPageRes.class)})
    public PageResult<StockRecordPageRes> listPage(String shopId, String storageId, String current, String pageSize, String keyword, String productType) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        if (StringUtils.isNotBlank(shopId)) {
            map.put("shopId", shopId);
        }
        map.put("productType", productType);
        map.put("storageId", storageId);
        map.put("companyId", companyId + "");
        map.put("current", current);
        map.put("pageSize", pageSize);
        return stockRecordService.getApiListPage(map);
    }


    @UserSessionRequired
    @GetMapping("init")
    @ApiOperation(value = "店铺仓库列表", notes = "店铺仓库列表")
    public SingleResult init() {
        Long companyId = getCompanyId();
        List<Map> mapList = shopService.selectShopListInit(null, companyId);
        List<Map> mapList1 = storageService.storageList();
        Map map = new HashMap();
        map.put("shopList", mapList);
        map.put("storageList", mapList1);
        return SingleResult.success(map);
    }

    @UserSessionRequired
    @GetMapping("trackWay")
    @ApiOperation(value = "获取发货物流渠道", notes = "获取发货物流渠道")
    public SingleResult getTrackWay() {
        Long companyId = getCompanyId();
        return SingleResult.success(service.getTrackWay());
    }

    @UserSessionRequired
    @GetMapping("trackInfo")
    @ApiOperation(value = "获取订单詳情(追踪号)", notes = "获取订单詳情追踪号")
    public SingleResult getTrackInfo(String outOrderNumber) {
        Long companyId = getCompanyId();
        OrderOutputDetailRes detailRes = service.getTrackInfo(outOrderNumber, companyId);
        return SingleResult.success(detailRes);
    }

    @UserSessionRequired
    @GetMapping("stockLog")
    @ApiOperation(value = "获取库存流水", notes = "获取库存流水")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "type", value = "操作类型"),
            @ApiImplicitParam(name = "status", value = "产品类型"),
            @ApiImplicitParam(name = "orderType", value = "订单类型"),
            @ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间")})
    public PageResult getStockLog(String startDate,String endDate,String status,String type,String orderType,
                                    String current,String pageSize,String storageId,String shopId,String sku,String orderNumber) {
        Long companyId = getCompanyId();
        Map<String, String> map=new HashMap<>();
        map.put("orderNumber", orderNumber);
        map.put("sku", sku);
        if (StringUtils.isNotBlank(shopId)) {
            map.put("shopId", shopId);
        } else if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("storageId", storageId);
        map.put("status", status);
        map.put("type", type);
        map.put("orderType", orderType);
        map.put("companyId", companyId + "");
        map.put("current", current);
        map.put("pageSize", pageSize);
        return stockLogService.getAPIListPage(map);
    }


    @UserSessionRequired
    @GetMapping("cancel")
    @ApiOperation(value = "取消订单", notes = "取消订单")
    public SingleResult cancelOrder(String outOrderNumber) {
        return service.cancelOrder(outOrderNumber, getCurrentUserId());
    }

}
